﻿module Sustainalytics.EsgRatings {

    export class ESGProfileController {

        public $scope: ng.IScope;
        public UI: ESGProfileOutDto;
        public DefaultProfile: ESGProfileDto; 
        public isLoading: boolean;


        private notificationFactory: INotificationFactory;
        private $q: ng.IQService;
        private backend: ESGProfileBackendService;


        constructor($scope: ng.IScope, $rootScope, $q: ng.IQService, $location: ng.ILocationService, $http: ng.IHttpService,
            ESGProfileBackend, notificationFactory: INotificationFactory, staticObjects) {

            var ctx = this;
            ctx.$q = $q;
            ctx.$scope = $scope;
            ctx.backend = ESGProfileBackend;
            ctx.notificationFactory = notificationFactory;
            ctx.DefaultProfile = new ESGProfileDto();
            ctx.UI = new ESGProfileOutDto();


            ctx.loadDefaultProfile();


            $scope['esgProfile'] = this;

            // notify parents that the controller was created
            $scope.$emit('esgProfile.created', ctx);
        }


        public saveDefaultProfile() {
            // check if the form is valid
            var ctx = this;
            if (ctx.$scope['esgProfileForm'].$invalid) {
                ctx.notificationFactory.error('', "Invalid selection, please select at least one element in each list");
                return;
            }


            var matrixpair = ctx.UI.listMatrixes.filter(value => { return value.id == ctx.DefaultProfile.matrixId });
            if (matrixpair)
                ctx.DefaultProfile.matrixName = matrixpair.pop().name;


            var refUnivPair = ctx.UI.listReferenceUniverses.filter(value => { return value.id == ctx.DefaultProfile.referenceUniverseId });
            if (refUnivPair)
                ctx.DefaultProfile.referenceUniverseName = refUnivPair.pop().name;

            var def = this.$q.defer();

            ctx.backend
                .saveProfile(ctx.DefaultProfile)
                .success(data => { ctx.notificationFactory.success('Save succeeded'); def.resolve(data); })
                .error(err => { ctx.notificationFactory.error('', 'Save fails'); def.reject(); });

            return def.promise;
        }

        public loadDefaultProfile() {
            var ctx = this;
            var promise = ctx.backend.loadProfiles(true);

            promise
                .then(
                (response =>
                    {
                        ctx.UI = response.data.result;
                        var p = <ESGProfileDto>response.data.result.profiles[0];

                        ctx.DefaultProfile = p;
                        //ctx.DefaultProfile.matrixId = p.matrixId;                   
                        //ctx.DefaultProfile.referenceUniverseId = p.referenceUniverseId;                   
                        //ctx.DefaultProfile.companyType = p.companyType;                   
                        //ctx.DefaultProfile.comparativeGroup = p.comparativeGroup;                   
                        //ctx.DefaultProfile.templateType = p.templateType;                   
 

                    }),
                        (errorReason => { console.log(errorReason);  })
                 ).finally(() => { ctx.isLoading = false; });  
    
        }

    }

    // register controller with EsgRatings Angular module
    angular.module('Sustainalytics.EsgRatings')
        .controller('ESGProfileController', ESGProfileController);
} 